//
//  UIButton+Gray.m
//  CqlivingCloud
//
//  Created by xhl_dabaoMac on 2021/12/14.
//  Copyright © 2021 xinhualong. All rights reserved.
//

#import "UIButton+Gray.h"
#import "UIColor+Gray.h"
#import "UIResponder+Gray.h"
#import <objc/runtime.h>

@implementation UIButton (Gray)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        method_exchangeImplementations(class_getInstanceMethod(self, @selector(setTitleColor:forState:)), class_getInstanceMethod(self, @selector(c_setTitleColor:forState:)));
        method_exchangeImplementations(class_getInstanceMethod(self, @selector(setSelected:)), class_getInstanceMethod(self, @selector(c_setSelected:)));
    });
}

- (void)c_setTitleColor:(UIColor *)color forState:(UIControlState)state{
    [self c_setTitleColor: self.changed ? color.changeGray:color forState:state];
}

- (void)c_setSelected:(BOOL)selected{
    [self c_setSelected:selected];
    [self c_gray];
}
- (void)c_gray{
    if (self.changed) {
        [self setTitleColor:self.titleLabel.textColor.changeGray
                   forState:self.selected ? UIControlStateSelected: UIControlStateNormal];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self c_gray];
}
@end
